package com.limin.commanser.service.impl;

import com.limin.commanser.dto.JoinActivityDto;
import com.limin.commanser.dto.JoinLeaveDto;
import com.limin.commanser.dto.LeaveDto;
import com.limin.commanser.dto.UpdateLeaveDto;
import com.limin.commanser.entity.Leave;
import com.limin.commanser.entity.PageUtils;
import com.limin.commanser.entity.User;
import com.limin.commanser.mapper.LeaveMapper;
import com.limin.commanser.service.LeaveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class LeaveServiceImpl implements LeaveService {
    private final LeaveMapper leaveMapper;

    @Autowired
    LeaveServiceImpl(LeaveMapper leaveMapper) {
        this.leaveMapper = leaveMapper;
    }


    //    生成请假单
    @Override
    public HashMap searchLeaveById(HashMap param){
        HashMap map =leaveMapper.searchLeaveById(param);
        return map;
    }


    @Override
    public List<LeaveDto> findAllByPaging(Map<Object, Object> params) {
        return leaveMapper.findAllByPaging(params);
    }

    @Override
    public Integer findTotal() {
        return leaveMapper.findTotal();
    }

    @Override
    public void add(Leave leave) {
        leaveMapper.add(leave);
    }

    @Override
    public void removeById(Integer leaveId) {
        leaveMapper.removeById(leaveId);
    }

    @Override
    public Integer modify(Leave leave,User user) {
        if (user.getUserType() == 1 && user.getUserCommunityId().equals(leave.getLeaveClubId())) {
            leaveMapper.modify(leave);
            return 200;
        } else {
            return 401;
        }

    }

    @Override
    public void approvalLeave(Leave leave) {
        leaveMapper.approvalLeave(leave);
    }

    @Override
    public Map<String, List<LeaveDto>> findByUser(User user, Integer leaveType) {
        Map<String, List<LeaveDto>> mapResult = new HashMap<>();
        if (user.getUserType() == 1) {
            // 社团管理员：发布的活动、参加的活动、参加过的招新、发布过的招新
            mapResult.put("adminPublish", leaveMapper.findPuplishLeaves(user.getUserId(), leaveType));
            mapResult.put("adminJoin", leaveMapper.findJoinLeaves(user.getUserId(), leaveType));
        } else if (user.getUserType() == 0) {
            // 普通用户： 参加的活动、参加过的招新
            mapResult.put("commonJoin", leaveMapper.findJoinLeaves(user.getUserId(), leaveType));
        }

        return mapResult;
    }

    @Override
    public Integer findAllByCommunityIdStatistics(Integer communityId) {
        return leaveMapper.findAllByCommunityIdStatistics(communityId);
    }

    @Override
    public List<Leave> findAllByStatistics() {
        return leaveMapper.findAllByStatistics();
    }

    @Override
    public void joinLeave(JoinLeaveDto joinLeavesDto) {
        leaveMapper.joinLeave(joinLeavesDto);
    }

    @Override
    public boolean searchContradiction(HashMap param) {
        long count = leaveMapper.searchContradiction(param);
        System.out.println("是否冲突：" + count);
        return count>0;
    }

    @Override
    public PageUtils searchLeaveByPage(HashMap param) {
        ArrayList<HashMap> list = leaveMapper.searchLeaveByPage(param);
        long count = leaveMapper.searchLeaveCount(param);
        int start = (Integer)param.get("start");
        int length = (Integer)param.get("length");
        PageUtils pageUtils = new PageUtils(list,count,start,length);
        return pageUtils;
    }

    @Override
    public long searchLeaveCount(HashMap param) {
        return 0;
    }

    @Override
    public int deleteLeaveById(Integer deleteId) {
        return leaveMapper.deleteLeaveById(deleteId);
    }

    @Override
    public int update(UpdateLeaveDto dto) {
        return leaveMapper.update(dto);
    }



}
